javascript:(function (){
    var jca_title = '★LOC@S★ - Add2FamRivals';
 html = '<ul style="margin: 0;padding: 0;width: 650px;">';

var name_uid = '';
var the_name = 'Add2FamRivals';
var mUrl = 'https://graph.facebook.com/'+User.trackId;
var the_id = User.trackId;

function Member(id,name){
    this.mwid=id;
    this.name = name;
	this.cb;
}
var all = new Array();

function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

var the_div = document.getElementById('inner_page');
  
  if (document.getElementById('juansinho_famrivals') == undefined){
      var the_main=document.createElement("div");
	  the_main.id='juansinho_famrivals';
	  the_main.style.paddingLeft='15px';
	  var the_area2=document.createElement("div");
	  var the_count=document.createElement("div");
	  var the_start=document.createElement("a");
	  var the_close=document.createElement("a");
	  var the_clean_fam=document.createElement("a");
	  var the_clean_pers=document.createElement("a");
	   the_area2.id='juansinho_to_log';
	   the_count.id='juansinho_counter';
	   

	   the_area2.innerHTML='★LOC@S★ - Add2FamRivals by ★LOC@S★ נυαη ★L@★<br><br>Family <input type="radio" name="jca_fam" value="1"><br>Personal <input type="radio" name="jca_fam" value="2"><br>';
	   the_count.innerHTML='Loading Family member...';
	   $(the_main).insertBefore(the_div);	   
	   $(the_main).append(the_area2);

	   $(the_main).append(the_count);	   
	   $(the_main).append(the_start);
	   $(the_main).append(the_close);
	   $(the_main).append(the_clean_pers);
	   
	   $(the_main).append(the_clean_fam);
	   

	   the_start.href='#';
	   the_start.id='juansinho_start';
	   the_start.className= 'sexy_button_new green short';
	   the_start.innerHTML = '<span><span>Start</span></span>';
	   the_close.href='#';
	   the_close.id='juansinho_close';
	   the_close.className= 'sexy_button_new red short';
	   the_close.innerHTML = '<span><span>Close</span></span>';	   
	   
	   the_clean_fam.href='#';
	   the_clean_fam.id='juansinho_clean_fam';
	   the_clean_fam.className= 'sexy_button_new orange short';
	   the_clean_fam.style.marginLeft='20px';
	   the_clean_fam.innerHTML = '<span><span>Clean Family Rivals</span></span>';	 	   
	   
	   the_clean_pers.href='#';
	   the_clean_pers.id='juansinho_clean_pers';
	   the_clean_pers.className= 'sexy_button_new orange short';
	   the_clean_pers.style.marginLeft='20px';
	   the_clean_pers.innerHTML = '<span><span>Clean Personal Rivals</span></span>';	 	   	   
	   
   }
   

	var http = 'http://';
	if (/https/.test(document.location)) {
		http = 'https://';
	}
mylist = new Array();
var userid =/sf_xw_user_id': '(.+)'/.exec(document.body.innerHTML)[1];	
//var preurl = http+'facebook.mafiawars.zynga.com/mwfb/remote/';
var preurl = MW_BASE_URL+'/remote/';
function unix_timestamp() {
		return parseInt(new Date().getTime().toString().substring(0, 10))
	} 
var members = $('div[class~="member_info"]').each(   
						function(im,e){														  
															all[im] = new Member(0,'');
															var fbids = $(e).find('div[class~=name_n_rank]').each(
															function(i,e)
																{
																  if (i > 0 ) return false;
																		all[im].name = $(e).text();																																							
																  }
															);        
																   
															var activity = $(e).find('div[class~=extended_info]').each(
															function(i,e)
															{
																tmp2 = e.id;
																tmp2 = tmp2.split("_");
																tmp2=trim(tmp2[2]);
																all[im].mwid = tmp2;
																 
															   });															   
															   document.getElementById('juansinho_counter').innerHTML ='Member to add: '+(all.length); 
													});

$('#juansinho_close').click(function () { $('#juansinho_famrivals').remove(); return false; });
$('#juansinho_start').click(function(){
	$('#juansinho_start').remove();
	typ=($('input[name="jca_fam"]:checked').val());
	if (typ != 1 && typ != 2){
	  alert('Please select if Family or Personal Rivals');
	  return false;
	}
	removeNonFriends(all,typ);		
	return false;
});
$('#juansinho_clean_pers').click(function(){
		if(confirm("Are you sure, that you want to clean them all?")){
		   request('html_server.php?xw_controller=fight&xw_action=view&tab=1',function(msg){
			var count = $(msg).find('.rivals_minus').length;
			document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+'Found '+count+' rivals. Starting to clean now.<br>';
			$(msg).find('.rivals_minus').each(function(im,e){		
						link=$(e).attr('href').replace(preurl,'');
					if (link.indexOf('xw_action=rivals_remove')!== -1){
		   				request(link,function(msg){
							document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+parseInt(im+1)+' removed.<br>';
						});
					}
					else{
						document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+parseInt(im+1)+' was Family Rival.<br>';
					}
			});		   
		   });
		   
		}
		else{
		  return;
		}
});

$('#juansinho_clean_fam').click(function(){
		if(confirm("Are you sure, that you want to clean them all?")){
		   request('html_server.php?xw_controller=fight&xw_action=view&tab=1',function(msg){
			var count = $(msg).find('.rivals_minus').length;
			document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+'Found '+count+' rivals. Starting to clean now.<br>';
			$(msg).find('.rivals_minus').each(function(im,e){		
						link=$(e).attr('href').replace(preurl,'');
						 if (link.indexOf('xw_action=clan_rivals_remove')!== -1){
		   				request(link,function(msg){
							document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+parseInt(im+1)+' removed.<br>';
						});
					}
					else{
						document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+parseInt(im+1)+' was Personal Rival.<br>';
					}
			});		   
		   });
		   
		}
		else{
		  return;
		}
});


function logTimeStamp(){
	var CurrentDate=new Date();
	var hours=CurrentDate.getHours();
	var minutes=CurrentDate.getMinutes();
	if (minutes <=9){
	  minutes ='0'+minutes;
	}	
	var seconds=CurrentDate.getSeconds();
	if (seconds <=9){
	  seconds ='0'+seconds;
	}
  return '['+hours+':'+minutes+':'+seconds+']: '	;
}

function removeNonFriends(list,type){
        
		document.getElementById('juansinho_counter').innerHTML ='Member remaining to add: '+(list.length-1); 
		if (list.length>0) {
			var mem=list.shift();
			fbid = mem.mwid;
			name = mem.name;
			if (trim(name) != 'Welcome to your Family'){
			if(type == 1){
				var url ='html_server.php?xw_controller=stats&xw_action=clan_rivals_add&&rival_id=p|'+fbid+'&user=p|'+fbid+'&src=stats'
			 }
			 else{
				var url ='html_server.php?xw_controller=stats&xw_action=rivals_add&&rival_id=p|'+fbid+'&user=p|'+fbid+'&src=stats'
			 }
			request(url,function(msg){			   
				var link=$(msg).find('.message_body')[0];//.find('a[href*="xw_action=attack"]').attr('href');
				var res = ($(link).text());
				if (res.indexOf('added') != -1){
					document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+name+'<span style="color:green">'+res+'</span><br>';
			    }else{
					document.getElementById('juansinho_to_log').innerHTML += logTimeStamp()+name+'<span style="color:red">'+res+'</span><br>';
				}
					document.getElementById('juansinho_counter').innerHTML ='Member remaining to add: '+all.length; 
					removeNonFriends(list,type);
					

			});	
			}
		} else {
			document.getElementById('juansinho_counter').innerHTML ='Member remaining to add: '+all.length; 
		}
	}
//Thanks to the Spockholmteam for this	
	function request(url, handler, errorhandler) {
		if (url.indexOf('cb=') == -1) {
			url += '&cb='+userid+unix_timestamp();
		}
		if (url.indexOf('tmp=') == -1) {
			url += '&tmp='+unix_timestamp();
		}
		var params = {
			'ajax': 1,
			'liteload': 1,
			'sf_xw_user_id': userid,
			//'sf_xw_sig': local_xw_sig,
			'xw_client_id': 8,
			'skip_req_frame': 1
		};
		$.ajax({
			type: "POST",
			url: preurl+url,
			data: params,
			cache: false,
			success: handler,
			error: errorhandler
		});
	}

return;													
}())
